package com.wcar.sale.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.User;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.sale.entity.Store;
import com.wcar.sale.mapper.StoreMapper;
import com.wcar.sale.service.StoreService;
import com.wcar.sale.vo.Nachu;
import com.wcar.sale.vo.Querysum;
import com.wcar.sale.vo.QueryParam;
import com.wcar.sale.vo.saleTN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author DELL
 * @description 针对表【tb_store(门店)】的数据库操作Service实现
 * @createDate 2024-05-24 10:34:51
 */
@Service
public class StoreServiceImpl implements StoreService {
	@Resource
	private StoreMapper storeMapper;



	@Autowired
	private StringRedisTemplate redisTemplate ;

	@Override
	public Result<Querysum> getall(QueryParam queryParam) {
		ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = sra.getRequest();
		String token1 = request.getHeader("token");
		String s = redisTemplate.opsForValue().get("user:login:" + token1);
		User user = JSON.parseObject(s, User.class);
		//拿到名称
		List<String> zhaodenluren=storeMapper.zhaodenluren1();
		if (user.getUserId()!=1) {
			//找到登录人的门店id
			zhaodenluren = storeMapper.zhaodenluren(user.getUserName());
		}
		zhaodenluren.forEach(System.out::println);
		//所有人进来不会为null

		//new 返回对象,service改业务,使返回值与前端要求对应
		Querysum querysum = new Querysum();
		//获得当前时间
		DateTime date = DateUtil.date();
		//当前时间转String
		String format = DateUtil.format(date, "yyyyMM");
		//获得拿到的时间
		String date1 = queryParam.getDate();
		//返回前端一份
		querysum.setDate(date1);
		//判断如果controller层
		/*if (queryParam.getDate().equals(format)) {
			//给后端,为了使数据好看,刚进去展示总金额
			queryParam.setDate(null);
		}*/
		//获得所有门店(可以通过门店名(日期视情况))查)
		ArrayList<String> storeName = new ArrayList<>();
		List<Nachu> nachus = storeMapper.gatstoreName(queryParam);
		for (Nachu a: nachus) {
			if (zhaodenluren.contains(a.getCc())) {
				storeName.add(a.getAa());
			}

		}
		//放入门店名,用ArrayList可以确保与数据对应
		querysum.setStoreName(storeName);
		//获得门店对应的营业额(通过门店名与日期查)
		ArrayList<Integer> sum = new ArrayList<>();
		List<Nachu> nachus1 = storeMapper.gatNum(queryParam);
		//双for循环,因为都是所有门店都会遍历出来(即使没有营业额)

		//判断是否有相同的zhaodenluren与nachus 一样才输出


		for (Nachu a: nachus) {
			for (Nachu b: nachus1) {
				//不包含结束
				if (zhaodenluren.contains(a.getCc())) {
					//通过判断使值相加
					if (a.getCc().equals(b.getCc())){
						try {
							//null+...=null ,会出现数据消失的问题(sql的局限性原因)
							if (a.getBb()==null && b.getBb()==null ) {
								sum.add(0);
							}
							if (a.getBb()==null && b.getBb()!=null ) {
								sum.add(b.getBb());
							}
							if (a.getBb()!=null && b.getBb()==null ) {
								sum.add(a.getBb());
							}
							if (a.getBb()!=null || b.getBb()!=null) {
								sum.add(a.getBb()+b.getBb());
							}
						} catch (Exception e) {
						}
					}
				}


			}
		}
		//放入店对应的数据
		querysum.setSum(sum);
		return new Result(200,"所有店铺营业金额",querysum) ;
	}

	@Override
	public Result getallStroe() {
		QueryWrapper<Store> wrapper = new QueryWrapper<>();
		ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = sra.getRequest();
		String token1 = request.getHeader("token");
		String s = redisTemplate.opsForValue().get("user:login:" + token1);
		User user = JSON.parseObject(s, User.class);
		System.out.println(user.getCreateBy()+"============================");
		if (user.getUserId()!=1) {
			wrapper.eq("create_by", user.getUserName());
		}
		List<Store> stores = storeMapper.selectList(wrapper);
		return new Result(200,"遍历所有店铺",stores );
	}
	@Override
	public String  zhaoid(String aa) {
		return storeMapper.zhaoid(aa);
	}

	@Override
	public Result<saleTN> getallM() {
		List<saleTN> selectall = storeMapper.selectall();
		System.out.println(selectall);
		return new Result(2000,"拿取客户统计信息",selectall);
	}

	public static void main(String[] args) {
		DateTime date = DateUtil.date();
		String format = DateUtil.format(date, "yyyyMM");
		System.out.println(format);
		DateTime parse = DateUtil.parse(format);
		System.out.println("2023-12-31T16:00:00.000".substring(0, 7).replace("-",""));


	}
}





